package org.zxp.javacheck.map;

import com.alibaba.fastjson.JSON;
import lombok.Builder;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @program: effectiveJava
 * @description:
 * @author: X-Pacific zhang
 * @create: 2020-08-26 10:00
 **/
public class Test {
    public static void main(String[] args) throws Exception {

        //Map<String, List<Inner>> map = new HashMap<>();
        //List<Inner> list1 = Arrays.asList(Inner.builder().type("clientType").name("个人").value("1").build(),Inner.builder().type("clientType").name("企业").value("2").build());
        //List<Inner> list2 = Arrays.asList(Inner.builder().type("isLoop").name("是").value("1").build(),Inner.builder().type("isLoop").name("否").value("2").build());
        //
        //map.put("clientType",list1);
        //map.put("isLoop",list2);
        //System.out.println(JSON.toJSONString(map));

        Map<String, Map<String,String>> map = new HashMap<>();
        Map<String,String> m1 = new HashMap<>();
        Map<String,String> m2 = new HashMap<>();
        m1.put("1","个人");
        m1.put("2","企业");
        m2.put("1","是");
        m2.put("2","否");
        map.put("clientType",m1);
        map.put("isLoop",m2);
        System.out.println(JSON.toJSONString(map));



        Map<String, Map<String,String>> mapResult = new HashMap<>();
        Stream.of(Inner.builder().type("clientType").name("个人").value("1").build(),
                Inner.builder().type("clientType").name("企业").value("2").build(),
                Inner.builder().type("isLoop").name("是").value("1").build(),
                Inner.builder().type("isLoop").name("否").value("2").build()
        ).collect(Collectors.groupingBy(Inner::getType)).forEach((k,v) -> {
            mapResult.put(k,v.stream().collect(Collectors.toMap(Inner::getValue, Inner::getName)));
        });
        System.out.println(JSON.toJSONString(mapResult));


    }
    @Data
    @Builder
    private static class Inner{
        private String type;
        private String value;
        private String name;
    }
}
